﻿#include "ssTerrain10Render.h"


namespace StarSeeker
{

ssTerrain10Render::ssTerrain10Render()
{
	m_queTag = SS_QUE_TERRAIN;
	m_gridX = 0;
	m_gridY = 0;
	m_gridLength = 0.0f;
}

ssTerrain10Render::~ssTerrain10Render()
{
}

bool ssTerrain10Render::BuildGrid(u32 row, u32 column, ssfloat grLen)
{
	m_gridX = column;
	m_gridY = row;
	m_gridLength = grLen;
	u32 nXVertCount = m_gridX + 1;
	u32 nYVertCount = m_gridY + 1;
	//顶点和纹理坐标数据
	ssArray<ssfloat> arrVertex;
	arrVertex.Init(nXVertCount*nYVertCount * 3);
	ssArray<ssfloat> arrTexCoord;
	arrTexCoord.Init(nXVertCount*nYVertCount * 2 * 2);
	ssfloat temp;
	for (u32 iy = 0; iy < nYVertCount; ++iy)
	{
		for (u32 ix = 0; ix < nXVertCount; ++ix)
		{
			//顶点
			temp = (ssfloat)ix*m_gridLength;
			arrVertex.PushBack(temp);
			temp = (ssfloat)iy*m_gridLength;
			arrVertex.PushBack(temp);
			temp = 0.0f;
			arrVertex.PushBack(temp);
			//纹理坐标1
			temp = (ssfloat)ix / (ssfloat)m_gridX;
			arrTexCoord.PushBack(temp);
			temp = (ssfloat)iy / (ssfloat)m_gridY;
			arrTexCoord.PushBack(temp);
			//纹理坐标2
			temp = (ssfloat)ix*m_gridLength / 512.0f;
			arrTexCoord.PushBack(temp);
			temp = (ssfloat)iy*m_gridLength / 512.0f;
			arrTexCoord.PushBack(temp);
		}
	}
	//索引数据
	ssArray<u32> arrIndex;
	arrIndex.Init(m_gridX*m_gridY * 6);
	u32 tmpU32 = 0;
	u32 nLocIndex = 0;
	for (u32 iy = 0; iy < nYVertCount - 1; ++iy)
	{
		for (u32 ix = 0; ix < nXVertCount - 1; ++ix)
		{
			nLocIndex = iy * nXVertCount + ix;
			tmpU32 = nLocIndex;
			arrIndex.PushBack(tmpU32);
			tmpU32 = nLocIndex + 1;
			arrIndex.PushBack(tmpU32);
			tmpU32 = nLocIndex + nXVertCount;
			arrIndex.PushBack(tmpU32);
			tmpU32 = nLocIndex + nXVertCount;
			arrIndex.PushBack(tmpU32);
			tmpU32 = nLocIndex + 1;
			arrIndex.PushBack(tmpU32);
			tmpU32 = nLocIndex + nXVertCount + 1;
			arrIndex.PushBack(tmpU32);
		}
	}
	arrIndex.PushBackCP(0);
	arrIndex.PushBackCP(1);
	arrIndex.PushBackCP(3);
	arrIndex.PushBackCP(3);
	arrIndex.PushBackCP(1);
	arrIndex.PushBackCP(4);
	//创建VBO
	if (!SetVertex(arrVertex.Count(), arrVertex.Memory()))
	{
		return false;
	}
	if (!SetIndex(arrIndex.Count(), arrIndex.Memory(), SS_INDEX_32))
	{
		return false;
	}
	if (!SetTextureCoord(2, arrTexCoord.Count(), arrTexCoord.Memory()))
	{
		return false;
	}
	return true;
}

}
